package com.linjian.togetherbirthday.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.bean.StoreListBean;
import com.linjian.togetherbirthday.view.ObservableScrollView;

public class FoodInfoFragment extends BaseFragment {

    private int movieId;
    private ImageView iv_bg;
    private RelativeLayout rl_title;
    private int height;
    private SwipeRefreshLayout srl_refresh;
    private TextView tv_abstract;
    private boolean isHide = true;
    private RelativeLayout rl_abstract;
    private ImageView iv_hide;
    private ImageView iv_icon;
    private RecyclerView rl_comment;
    private TextView tv_comment_num;
    private TextView tv_title_name;
    private RecyclerView rl_stars;
    private ImageView iv_back;
    private ObservableScrollView ob_scrollView;
    private ImageView storeIv;
    private TextView storeNameTv,storeClassTv;

    public static FoodInfoFragment newInstance(String data){
        FoodInfoFragment foodInfoFragment = new FoodInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("info",data);
        foodInfoFragment.setArguments(bundle);
        return foodInfoFragment;
    }

    @Override
    protected int setView() {
        return R.layout.fragment_food_info;
    }

    @Override
    protected void init(View view) {
        iv_bg = view.findViewById(R.id.iv_bg);
        tv_abstract = view.findViewById(R.id.tv_abstract);
        rl_title = view.findViewById(R.id.rl_title);
        RelativeLayout rl_info = view.findViewById(R.id.rl_info);
        ob_scrollView = view.findViewById(R.id.ob_scrollView);

        rl_abstract = (RelativeLayout) view.findViewById(R.id.rl_abstract);
        iv_hide = (ImageView) view.findViewById(R.id.iv_hide);
        rl_comment = (RecyclerView) view.findViewById(R.id.rl_comment);
        iv_back = (ImageView) view.findViewById(R.id.iv_back);
        tv_title_name = (TextView) view.findViewById(R.id.tv_title_name);
        srl_refresh = (SwipeRefreshLayout) view.findViewById(R.id.srl_refresh);
        tv_comment_num = (TextView) view.findViewById(R.id.tv_comment_num);
        rl_stars = (RecyclerView) view.findViewById(R.id.rl_stars);
        storeIv = view.findViewById(R.id.food_info_iv);
        storeNameTv = view.findViewById(R.id.food_info_store_name);
        storeClassTv = view.findViewById(R.id.food_info_class);

        rl_title.setBackgroundColor(Color.argb(0, 220, 14, 60));
        height = rl_info.getLayoutParams().height - rl_title.getLayoutParams().height;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

        StoreListBean.DataBean.RecordsBean recordsBean = new Gson().fromJson(getArguments().getString("info"),StoreListBean.DataBean.RecordsBean.class);
        Glide.with(getActivity()).load(recordsBean.getPhoto_urls()).error(R.mipmap.user_pic).placeholder(R.mipmap.user_pic).into(storeIv);
        storeNameTv.setText(recordsBean.getName());
        String categories = "";
        for (int i = 0; i < recordsBean.getCategories().size(); i++) {
            categories = categories+recordsBean.getCategories().get(i)+"   ";
        }
        storeClassTv.setText(categories);

        ob_scrollView.setOnScrollChangedListener(new ObservableScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                tv_title_name.setVisibility(View.GONE);
                if (y < 0)
                    return;
                if (y >= height / 2 && y <= height - height / 4) {
                    tv_title_name.setVisibility(View.VISIBLE);
//                    float scale = (float) y / height;
//                    float alpha = (255 * scale);
//                    rl_title.setBackgroundColor(Color.argb((int) alpha, 220, 14, 60));
                    rl_title.setBackgroundResource(R.mipmap.toolbar_bg);
                    rl_title.setAlpha(0.5f);
                } else if (y >= 0 && y < height / 2) {
                    rl_title.setBackgroundColor(Color.argb(0, 220, 14, 60));
//                    rl_title.setBackgroundResource(R.mipmap.toolbar_bg);
//                    rl_title.setAlpha(0f);
                } else {
                    tv_title_name.setVisibility(View.VISIBLE);
                    rl_title.setBackgroundResource(R.mipmap.toolbar_bg);
                    rl_title.setAlpha(1f);
//                    rl_title.setBackgroundColor(Color.argb(255, 220, 14, 60));
                }
            }
        });


        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });

        srl_refresh.setColorSchemeResources(R.color.colorPrimary, R.color.black);
        srl_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });


        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rl_comment.setLayoutManager(layoutManager);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rl_stars.setLayoutManager(linearLayoutManager);

    }
}
